package com.telecom.cms.actions;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.telecom.cms.dao.SEG_BIZ_ATOM_DEFDAO;
import com.telecom.cms.util.Constant;
import com.telecom.cms.dao.GetConnect;

import com.telecom.cms.dto.*;

/**
 * @author yueyingnishang
 * 
 */
public class TopicreviewAction extends Action {

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping,
	 *      org.apache.struts.action.ActionForm,
	 *      javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		request.getSession().removeAttribute("AtomList");
		request.getSession().removeAttribute("Outputlist");

		int folderid = Integer.parseInt(request.getParameter("id"));
		Connection conn = GetConnect.getCsConnection();
		PreparedStatement psmt2 = conn
				.prepareStatement(" select APP_SUBJECT_ID from "
						+ Constant.SEG_APP_SUBJECT_DEF + " where FOLEDER_ID = "
						+ folderid);
		ResultSet rs2 = psmt2.executeQuery();
		int APP_SUBJECT_ID = 0;
		if (rs2.next()) {

			APP_SUBJECT_ID = rs2.getInt(1);

		}
		if (rs2 != null) {

			rs2.close();
		}
		if (psmt2 != null) {

			psmt2.close();
		}

		// 取原子限制条件
		String sql1 = "select * from " + Constant.SEG_SEG_RULE_DEF
				+ " where  APP_SUBJECT_ID = " + APP_SUBJECT_ID;

		PreparedStatement psmt = conn.prepareStatement(sql1);
		ResultSet rs = psmt.executeQuery();
		List atomlist = new ArrayList();
		while (rs.next()) {

			ShowatomDTO dto = new ShowatomDTO();

			dto.setAtomid(rs.getInt(4));
			dto.setAtomname(rs.getString(5));
			dto.setAtomvalue(rs.getString(6));
			dto.setChinesevalue(rs.getString(7));

			atomlist.add(dto);
		}

		// 取展示原子

		String sql2 = "select * from " + Constant.SEG_SEG_RULE_SHOWATOM
				+ " where  APP_SUBJECT_ID = " + APP_SUBJECT_ID;

		PreparedStatement ps1 = conn.prepareStatement(sql2);
		ResultSet rs1 = ps1.executeQuery();

		List sqllist = new ArrayList();
		while (rs1.next()) {
			int atomid = rs1.getInt(1);
			SEG_BIZ_ATOM_DEFDAO dao = new SEG_BIZ_ATOM_DEFDAO(conn);
			SEG_BIZ_ATOM_DEFDTO seg_BIZ_ATOM_DEFDTO = new SEG_BIZ_ATOM_DEFDTO();
			seg_BIZ_ATOM_DEFDTO = dao.findById(atomid);
			sqllist.add(seg_BIZ_ATOM_DEFDTO);
		}

		request.getSession().setAttribute("Outputlist", sqllist);
		request.getSession().setAttribute("AtomList", atomlist);
		request.setAttribute("id", folderid);
		if (request.getSession().getAttribute("APP_SUBJECT_ID") != null) {
			request.getSession().removeAttribute("APP_SUBJECT_ID");
		}
		request.getSession().setAttribute("APP_SUBJECT_ID",new Integer(APP_SUBJECT_ID));
		
		request.setAttribute("retainCSSession", "yes");

		return mapping.findForward("success");
	}

}
